package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.CollectionUtils;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.dynamic.mapper.RoomGiftRecordMapper;
import com.ruoyi.dynamic.model.pojo.entity.RoomGiftRecord;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomRankSearchRequest;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomRankUserInfoResult;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomUserInfoResult;
import com.ruoyi.dynamic.service.IRoomGiftRecordService;
import com.ruoyi.system.api.RemoteAppUserService;
import com.ruoyi.system.api.model.user.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 房间赠送礼物记录表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-11-30
 */
@Service
public class RoomGiftRecordServiceImpl extends ServiceImpl<RoomGiftRecordMapper, RoomGiftRecord> implements IRoomGiftRecordService {

    @Autowired
    private RoomGiftRecordMapper giftRecordMapper;
    @Autowired
    private RemoteAppUserService remoteAppUserService;


    @Async
    @Override
    public void addRecord(Long roomId, String roomNumber, Long userId, Set<Long> aimUserId, Long gift, String remark, BigDecimal money, Integer contrNum) {
        R<AppUser> userInfo = remoteAppUserService.getUserInfo(userId, SecurityConstants.APP);
        AppUser data = userInfo.getData();
        List<RoomGiftRecord> collect = aimUserId.stream().map(res -> {
            R<AppUser> aimUser = remoteAppUserService.getUserInfo(res, SecurityConstants.APP);
            AppUser data1 = aimUser.getData();
            RoomGiftRecord record = new RoomGiftRecord();
            record.setRoomId(roomId);
            record.setRoomNumber(roomNumber);
            record.setUserId(userId);
            record.setAimUserId(res);
            record.setGiftId(gift);
            record.setRemark(remark);
            record.setMoney(money);
            record.setContrNum(contrNum);
            record.setUserNickName(data.getNickName());
            record.setUserPhoto(data.getPhoto());
            record.setUserSex(data.getSex());
            record.setNumber(1);
            record.setAimUserNickName(data1.getNickName());
            record.setAimUserPhoto(data1.getPhoto());
            return record;
        }).collect(Collectors.toList());
        saveBatch(collect);
    }

    @Override
    public Integer countGiveCount(Long id) {
        return lambdaQuery().eq(RoomGiftRecord::getRoomId, id).list().stream().map(RoomGiftRecord::getUserId).collect(Collectors.toSet()).size();
    }

    @Override
    public Integer getGiftGiveNumberByRoomId(Long roomId, Long giftId, Date time) {
        return lambdaQuery()
                .eq(RoomGiftRecord::getRoomId, roomId)
                .eq(RoomGiftRecord::getGiftId, giftId)
                .ge(!CommonUtils.isNull(time), RoomGiftRecord::getCreateTime, time)
                .list().stream().mapToInt(RoomGiftRecord::getNumber).sum();
    }

    @Override
    public AppRoomRankUserInfoResult listPage(AppRoomRankSearchRequest request, Long userId) {
        R<AppUser> userInfo = remoteAppUserService.getUserInfo(userId, SecurityConstants.APP);
        AppUser data = userInfo.getData();
        Set<Long> userIds = CollectionUtils.emptySet();
        if (request.getLabel() == 1) {
            R<Set<Long>> newUser = remoteAppUserService.getNewUser(SecurityConstants.APP);
            userIds = newUser.getData();
        }
        if (request.getLabel() == 2) {
            R<Set<Long>> newUser = remoteAppUserService.getCityWide(userId, SecurityConstants.APP);
            userIds = newUser.getData();
        }
        List<AppRoomUserInfoResult> list = giftRecordMapper.listPage(request, userIds);
        int index = -1;
        index = list.stream().filter(e -> e.getId().equals(userId)).map(list::indexOf).findFirst().orElse(-1);
        return AppRoomRankUserInfoResult.builder()
                .id(userId)
                .nickName(data.getNickName())
                .photo(data.getPhoto())
                .userInfoResults(list)
                .number(index == -1 ? 0 : index + 1)
                .money(BigDecimal.ZERO)
                .build();
    }
}
